package vek.controllers.Map;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import vek.GUI.Infoview.InfoView;
import vek.GUI.MapView.MarkingButton;
import vek.ObjectDatabase.DatabaseConnector;
import vek.Objects.POI;

/**
 * This class handles user input in the MarkingsView. 
 * When a user clicks on a POI this class makes sure that the POI info is shown.
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public class MarkingsViewController implements ActionListener
{
    private InfoView infoview;
    private DatabaseConnector databaseConnector;
    
    /**
     * 
     * @param iv the InfoView object which shows the POI info.
     * @param dbc the database connector which connects this object to the database
     */
    
    public MarkingsViewController(InfoView iv, DatabaseConnector dbc)
    {
        infoview = iv;
        databaseConnector = dbc;
    }
    
    /**
     * This method runs every time the end user clicks a MarkingButton.
     * This method makes sure that POI info is shown when the POI button is clicked.
     * @param e the ActionEvent created by the users click
     */
    public void actionPerformed(ActionEvent e) 
    {
        MarkingButton button = (MarkingButton) e.getSource();
        POI poi = button.getPOI();
        infoview.showInfo(getPOIDescription(poi), poi.getURLShown());
        
    }
    
    /**
     * Gets the various data about the POI that is supposed to be shown in the GUI.
     * @param poi the POI which is to be shown
     * @return String to be shown in the GUI with data from the POI
     */
    
    private String getPOIDescription(POI poi)
    {
        String returnedDesc = "";
        returnedDesc += "<h2>" + poi.getName();
        returnedDesc += "<p>";
        returnedDesc += poi.getAddresse().getShortAdresse();
        returnedDesc += "<br>";
        returnedDesc += "Telefonnummer: " + poi.getPhoneNumber();
        returnedDesc += "<p>";
        returnedDesc += poi.getDescription();
        returnedDesc += "<br>";
        returnedDesc += "Hjemmeside: " + poi.getURL();
        returnedDesc += "<br>";
        if(poi.getTypesOfProductsSold() != null)
        {
             returnedDesc += "Typer af produkter solgt: " + makePOIArrayListDataIntoString(poi.getTypesOfProductsSold());
             returnedDesc += "<br>";
        }
        if(poi.getTypesOfFoodSold() != null)
        {
             returnedDesc += "Slags mad solgt: " + makePOIArrayListDataIntoString(poi.getTypesOfFoodSold());
             returnedDesc += "<br>";
        }
        if(poi.getTypesOfExhibitions() != null)
        {
             returnedDesc += "Udstillingstyper: " + makePOIArrayListDataIntoString(poi.getTypesOfExhibitions());
             returnedDesc += "<br>";
        }
        returnedDesc += "<p>";
        returnedDesc += "<small>" + "Sidst opdateret: " + poi.getLastUpdated();
        return returnedDesc; 
    }
    
    /**
     * Takes an ArrayList containing strings, and outputs them as a single string for showing in gui
     * @param al ArrayList containing Strings
     * @return String containing all the strings in the ArrayList
     */
    
    private String makePOIArrayListDataIntoString(ArrayList<String> al)
    {
        String returnedString = "";
        for(int i = 0; i < al.size(); i++)
        {
            returnedString += al.get(i) + ", ";
        }
        return returnedString.substring(0, returnedString.length() - 2);
        
    }

}
